package edu.miami.group.photolog;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.ImageView;

/**
 * This class non-instantiable class is to hold the static recycleView
 * method so all activities can use it.
 * @author logan
 *
 */
public class Recycler {

	/**
	 * Suppress default constructor to insure noninstantiability.
	 */
	private Recycler() {
	}

	/**
	 * This function is to prevent memory issues with using Bitmaps.  --Copypasta--
	 * @param view
	 */
	public static void recycleView(View view) {
		ImageView imageView;
		Bitmap imageBitmap;
		BitmapDrawable imageBitmapDrawable;
		if (view != null) {
			if (view instanceof ImageView) {
				imageView = (ImageView)view;
				if ((imageBitmapDrawable = 
						(BitmapDrawable)imageView.getDrawable()) != null &&
						(imageBitmap = imageBitmapDrawable.getBitmap()) != null) {
					imageBitmap.recycle();
				}
				imageView.setImageURI(null);
				imageView.setImageBitmap(null);
			}
			if ((imageBitmapDrawable = 
					(BitmapDrawable)view.getBackground()) != null &&
					(imageBitmap = imageBitmapDrawable.getBitmap()) != null) {
				imageBitmap.recycle();
			}
			view.setBackgroundDrawable(null);
			System.gc();
		}
	}

}
